package com.ruoyi.gpt.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.gpt.domain.bo.ChatTokenBo;
import com.ruoyi.gpt.domain.vo.ChatTokenVo;
import com.ruoyi.gpt.domain.ChatToken;
import com.ruoyi.gpt.mapper.ChatTokenMapper;
import com.ruoyi.gpt.service.IChatTokenService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * chatGpt的tokenService业务层处理
 *
 * @author ztx
 * @date 2023-03-17
 */
@RequiredArgsConstructor
@Service
public class ChatTokenServiceImpl implements IChatTokenService {

    private final ChatTokenMapper baseMapper;

    /**
     * 查询chatGpt的token
     */
    @Override
    public ChatTokenVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询chatGpt的token列表
     */
    @Override
    public TableDataInfo<ChatTokenVo> queryPageList(ChatTokenBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ChatToken> lqw = buildQueryWrapper(bo);
        Page<ChatTokenVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询chatGpt的token列表
     */
    @Override
    public List<ChatTokenVo> queryList(ChatTokenBo bo) {
        LambdaQueryWrapper<ChatToken> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    @Override
    public List<ChatTokenVo> queryListByIsUsed() {
        LambdaQueryWrapper<ChatToken> qw = Wrappers.lambdaQuery();
        qw.eq(ChatToken::getIsUsed,"1");
        return baseMapper.selectVoList(qw);
    }

    private LambdaQueryWrapper<ChatToken> buildQueryWrapper(ChatTokenBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ChatToken> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getDeptId() != null, ChatToken::getDeptId, bo.getDeptId());
        lqw.eq(bo.getUserId() != null, ChatToken::getUserId, bo.getUserId());
        lqw.like(StringUtils.isNotBlank(bo.getUsername()), ChatToken::getUsername, bo.getUsername());
        lqw.eq(StringUtils.isNotBlank(bo.getIsUsed()), ChatToken::getIsUsed, bo.getIsUsed());
        lqw.eq(StringUtils.isNotBlank(bo.getToken()), ChatToken::getToken, bo.getToken());
        return lqw;
    }

    /**
     * 新增chatGpt的token
     */
    @Override
    public Boolean insertByBo(ChatTokenBo bo) {
        ChatToken add = BeanUtil.toBean(bo, ChatToken.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改chatGpt的token
     */
    @Override
    public Boolean updateByBo(ChatTokenBo bo) {
        ChatToken update = BeanUtil.toBean(bo, ChatToken.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ChatToken entity){
        //TODO 做一些数据校验,如唯一约束
        //如果有数据权限 将部门id和用户id录入
        LoginUser loginUser = LoginHelper.getLoginUser();
        entity.setDeptId(loginUser.getDeptId());
        entity.setUserId(loginUser.getUserId());
    }

    /**
     * 批量删除chatGpt的token
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
